package com.hncboy.chatgpt.front.service.impl;



import com.hncboy.chatgpt.front.api.chat0714.OpenAiStreamClientS;
import com.hncboy.chatgpt.front.domain.*;
import com.hncboy.chatgpt.front.service.ChatAuditService;
import com.hncboy.chatgpt.front.service.ChatCacheService;
import com.hncboy.chatgpt.front.service.IProProgressService;
import com.hncboy.chatgpt.front.util.AjaxResultBox;
import com.hncboy.chatgpt.front.util.ChatParams;
import com.hncboy.chatgpt.front.util.ChatResult;
import com.hncboy.chatgpt.front.util.ChoiceModel;
import io.reactivex.Single;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;


/**
 * Service业务层处理
 *
 * @date 2023-04-24
 */
@Slf4j
@Service
public class ProProgressServiceImpl implements IProProgressService {


    @Autowired
    private OpenAiStreamClientS openAiClient;

    @Autowired
    private ChatAuditService auditService;

    @Autowired
    private ChatCacheService cacheService;


    @Override
    public AjaxResultBox sseChat(ProStateManagement proStateManagement) {
        List<String> ss = new ArrayList<>();
        //聊天模型：gpt-3.5
        LinkedList<Message> messages = new LinkedList<>();
        //加个判断chatId": "0"是答题模式
        if (proStateManagement.getChatId().equals("0")){
            Message system = Message.builder().role(Message.Role.SYSTEM).content("你是一个全能的答题小助手，question是问题，options是答案，从0依次对应答案，你只需要回答阿拉伯一位数字").build();
            messages.addFirst(system);
        }
        Message message = Message.builder().role(Message.Role.USER).content(proStateManagement.getContent()).build();
        messages.add(message);
        ChatCompletion chatCompletion = ChatCompletion
                .builder()
                .messages(messages)
                .model(ChatCompletion.Model.GPT_3_5_TURBO.getName())
                .build();
        ChatCompletionResponse chatCompletionResponse = openAiClient.chatCompletion(chatCompletion);
        chatCompletionResponse.getChoices().forEach(e -> {
            ss.add(e.getMessage().getContent());
            System.out.println(e.getMessage());
        });
        return AjaxResultBox.success(ss);
    }


    @Override
    public Message buildUserMessage(String content) {
        Message message = new Message();
        message.setRole(ChatRoleConst.USER);
        message.setContent(content);
        return message;
    }

    @Override
    public ChatResult doChat(ChatParams params, String chatId) {
        // 写入用户ID
        params.setUser(auditService.getUserId());
        //聊天模型：gpt-3.5
        ChatCompletion chatCompletion = ChatCompletion
                .builder()
                .messages(params.getMessages())
                .model(ChatCompletion.Model.GPT_3_5_TURBO.getName())
                .build();
        Single<ChatResult> response = openAiClient.chatCompletionSS(chatCompletion);
        ChatResult result = response.blockingGet();
        // 获取返回结果
        List<ChoiceModel> choices = result.getChoices();
        // 获取第一条结果
        ChoiceModel choice = choices.get(0);
        // 获取消息
        Message message = choice.getMessage();
        // 写入缓存
        log.info(message.toString());
        // 2023-04-10 用服务方法实现替代原对缓存的直接操作
        cacheService.write(chatId, message);
        // 返回结果
        return result;
    }

}
